package com.tengju.bff.interfaces.covercharge.util;

import com.tengju.bff.interfaces.covercharge.constant.CoverChargeEnum;
import com.tengju.bff.interfaces.shared.InterfaceException;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.bff.interfaces.shared.servlet.UserServletContext;
import com.tengju.user.domain.model.user.UserIdCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

@Slf4j
public class UserUtils {

    /**
     * 获取用户ID
     * @param request
     * @return
     */
    public static Integer getUserId(HttpServletRequest request){
        String userId = request.getHeader("userId");
        if(StringUtils.isNotEmpty(userId)){
            return Integer.parseInt(userId);
        }else{
            throw new RuntimeException("未获取到网关透传的userId");
        }
    }

    public static Integer getUserId(Integer IdCode){
        return IdCode-200000;
    }

    public static Integer getIdCode(Integer userId){
        return userId+200000;
    }

    /**
     * 获取idCode
     * @param request
     * @return
     */
    public static Integer getIdCode(HttpServletRequest request)  {

        UserServletContext servletContext = ServletContextUtil.getServletContext();
        try {
            UserIdCode userIdCode = servletContext.needIdCode();
            return (int)userIdCode.getValue();
        } catch (InterfaceException e) {
            throw new RuntimeException("未获取到网关透传的idCode");
        }

//        String userId = request.getHeader("userId");
//        if(StringUtils.isNotEmpty(userId)){
//            return Integer.parseInt(userId)+200000;
//        }else{
//            throw new RuntimeException("未获取到网关透传的userId");
//        }
    }

    /**
     * 获取平台标识
     * @param request
     * @return
     */
    public static String getPlatformCode(HttpServletRequest request){
        String from = getPlatFrom(request);
//        if("tj".equals(from)){
//
//        }else{
//            return CoverChargeEnum.ServiceAccountType.CUSTOMER_TYPE.getPlatform();
//        }
        return CoverChargeEnum.ServiceAccountType.MANAGE_TYPE.getPlatform();
    }

    public static String getPlatFrom(HttpServletRequest request){
//        String from = request.getHeader("from");
//        log.debug("from:{}",from);
//        if(StringUtils.isNotEmpty(from)){
//            return from;
//        }else{
//            throw new RuntimeException("未获取平台标识from");
//        }

        return CoverChargeEnum.ServiceAccountType.MANAGE_TYPE.getPlatform();
    }

}
